package com.wz.jiangsu.controller.go;

import cn.hutool.core.util.StrUtil;
import cn.hutool.json.JSONUtil;
import com.wz.jiangsu.bean.vo.SecurityEventDataCommonGoAssentDto;
import com.wz.jiangsu.bean.vo.SecurityEventDataCommonGoDto;
import com.wz.jiangsu.util.JsonUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.kafka.core.KafkaTemplate;
import org.springframework.kafka.support.SendResult;
import org.springframework.util.concurrent.FailureCallback;
import org.springframework.util.concurrent.SuccessCallback;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @program: wz
 * @author: wangzhou
 * @create: 2023-12-29 09:20
 * @description:
 **/
@RestController
@RequestMapping("/test")
public class GoTestController {
    @Value("${go.assent.test.topic}")
    private String assent_test;

    @Value("${netvine.wazuh.security.test.topic}")
    private String syslog_test;


    @Autowired
    private KafkaTemplate<String, Object> kafkaTemplate;

    /**
     * 向资产引擎推送消息
     *
     * @param dto 模仿 Kafaka 消息体的实体类
     */
    @PostMapping("/kafka/post/callbackOne/go/receive/assent")
    public String sendCallbackOneMessageMoney(@RequestBody SecurityEventDataCommonGoDto dto) {
        String message = StrUtil.toString(dto);
        String jsonStr = JSONUtil.toJsonStr(dto);
        // 格式化成为一行
        System.out.println("-------replaceAllBlank---------");
        String oneRowJson = JsonUtil.replaceAllBlank(jsonStr);
        System.out.println(oneRowJson);
        kafkaTemplate.send(assent_test, jsonStr).addCallback(new SuccessCallback<SendResult<String, Object>>() {
            //成功的回调
            @Override
            public void onSuccess(SendResult<String, Object> success) {
                // 消息发送到的topic
                String topic = success.getRecordMetadata().topic();
                // 消息发送到的分区
                int partition = success.getRecordMetadata().partition();
                // 消息在分区内的offset
                long offset = success.getRecordMetadata().offset();

                System.out.println("发送消息成功-安全日志:" + topic + "-" + partition + "-" + offset);
            }
        }, new FailureCallback() {
            //失败的回调
            @Override
            public void onFailure(Throwable throwable) {
                System.out.println("发送消息失败1:" + throwable.getMessage());
            }
        });
        return jsonStr;
    }

    /**
     * 资产引擎吐出来的消息需要给态势
     *
     * @param dto
     * @return
     */
    @PostMapping("/kafka/post/callbackOne/go/send/syslog")
    public String sendCallbackOneMessageMoneyWazuh(@RequestBody SecurityEventDataCommonGoAssentDto dto) {
        String message = StrUtil.toString(dto);
        String jsonStr = JSONUtil.toJsonStr(dto);
        // 格式化成为一行
        System.out.println("-------replaceAllBlank---------");
        String oneRowJson = JsonUtil.replaceAllBlank(jsonStr);
        System.out.println(oneRowJson);
        kafkaTemplate.send(syslog_test, jsonStr).addCallback(new SuccessCallback<SendResult<String, Object>>() {
            //成功的回调
            @Override
            public void onSuccess(SendResult<String, Object> success) {
                // 消息发送到的topic
                String topic = success.getRecordMetadata().topic();
                // 消息发送到的分区
                int partition = success.getRecordMetadata().partition();
                // 消息在分区内的offset
                long offset = success.getRecordMetadata().offset();

                System.out.println("发送消息成功-安全日志:" + topic + "-" + partition + "-" + offset);
            }
        }, new FailureCallback() {
            //失败的回调
            @Override
            public void onFailure(Throwable throwable) {
                System.out.println("发送消息失败1:" + throwable.getMessage());
            }
        });
        return jsonStr;
    }
}
